<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="form.order_no"
            placeholder="订单号"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.status"
            placeholder="退款状态"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in statusList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款时间">
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column
            prop="merchant_id"
            label="商户id"
            align="center"
            width="120"
          />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
            width="150"
          />
          <el-table-column
            prop="order_no"
            label="系统订单号"
            align="center"
            width="250"
          />
          <el-table-column
            prop="merchant_order_no"
            label="下游商户订单号"
            align="center"
            width="250"
          />
          <el-table-column
            prop="channel_order_no"
            label="上游支付通道订单号"
            align="center"
            width="250"
          />
          <el-table-column
            prop="amount"
            label="退款金额"
            align="center"
            width="120"
          >
            <template #default="scope"
              >{{ scope.row.amount }} {{ scope.row.currency }}</template
            >
          </el-table-column>
          <el-table-column prop="amount" label="手续费" align="center">
            <template #default="scope">{{ scope.row.fee }}</template>
          </el-table-column>
          <el-table-column
            prop="settlement_amount"
            label="账户扣除金额"
            align="center"
            width="120"
          >
            <template #default="scope"
              >{{ scope.row.settlement_amount }}
              {{ scope.row.settlement_currency }}</template
            >
          </el-table-column>
          <el-table-column
            prop="arn_number"
            label="收单机构参考号"
            align="center"
          />
          <el-table-column prop="status" label="退款状态" align="center" />
          <el-table-column
            prop="sys_review_status"
            label="审核状态"
            align="center"
          />
          <el-table-column prop="description" label="备注" align="center" />
          <el-table-column
            prop="createtime"
            label="退款时间"
            :formatter="formatterTime"
            align="center"
            width="180"
          ></el-table-column>
          <el-table-column
            prop="finish_time"
            label="完成时间"
            :formatter="formatterTime"
            align="center"
            width="180"
          ></el-table-column>
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="120"
          >
            <template #default="scope">
              <el-button
                type="info"
                size="small"
                @click="handleOperateDesc(scope.row)"
                >备注</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <el-dialog v-model="descVisible" title="设置备注" width="500">
      <el-form :model="descForm">
        <el-form-item label="备注">
          <el-input v-model="descForm.description" type="textarea" rows="5" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="descVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="submitDesc(descForm)"
            :loading="btnLoading"
          >
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, pickerDefaultTime } from "@/utils/utils";
import { payOrderRefundApi } from "@/api/payServerse/payorder";
const loading = ref(false);
const tableData = ref([]);
const statusList = ref([]);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const btnType = ref(null);
const formData = {
  merchant_id: "",
  order_no: "",
  status: "",
  timeData: [],
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    order_no: form.order_no,
    status: form.status,
    merchant_id: form.merchant_id,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await payOrderRefundApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    statusList.value = res.data.extra.status.map((item) => {
      return { type: item.value, name: item.name };
    });
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};

const descForm = reactive({
  order_no: "",
  description: "",
});

const descVisible = ref(false);
const handleOperateDesc = async (row) => {
  // 编辑
  descVisible.value = true;
  Object.assign(descForm, {
    order_no: row.order_no,
    description: row.description,
  });
};

// 提交
const submitDesc = async (param) => {
  try {
    btnLoading.value = true;
    if (!param.description) {
      ElMessage({
        type: "error",
        message: "请填写备注",
      });
      btnLoading.value = false;
      return false;
    }
    let res;
    res = await setOrderDescApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    descVisible.value = false;
    btnLoading.value = false;
    getTableData();
  } catch (error) {
    btnLoading.value = false;
  }
};

onMounted(() => {
  getTbaleData();
});
</script>
